﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core;
using Aliyun.Acs.Sms.Model.V20160927;
using Aliyun.Acs.Core.Exceptions;

namespace Heyu.Utility
{
    /// <summary>
    /// 手机操作类
    /// </summary>
    public class MobileUtility
    {
        /// <summary>
        /// 返回的节点名称
        /// </summary>
        public enum ReturnNode
        {
            returnstatus,
            message,
            remainpoint,
            taskID,
            successCounts
        };

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string SendSms(string url, out string message)
        {
            string res = "";
            message = "";

            //string url = "http://sms.zy28.com:8888/sms.aspx?action=send&userid=308&account=cdmz&password=123456&mobile=" + mobile + "&content=" + content;

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc = HttpRequestUtility.GetXmlData(url);

                res = xmlDoc.SelectSingleNode("returnsms/returnstatus").InnerText;

                message = xmlDoc.SelectSingleNode("returnsms/message").InnerText;

                //XmlNodeList nodeList = xmlDoc.SelectNodes("returnsms/returnstatus");
                //res = nodeList[0].InnerText;
            }
            catch (Exception exp)
            {
                res = exp.ToString();
            }

            return res;

        }
        public static string SendSms(string url)
        {
            string message = "";
            return SendSms(url, out message);
        }

        //
        public static string SendMsg(string fxPhone, string fxPassword, string toPhone, string msg)
        {
            try
            {
                string url = "u=" + fxPhone + "&";
                url = url + "p=";
                url = url + fxPassword + "&";
                url = url + "to=";
                url = url + toPhone + "&";
                url = url + "m=" + msg;

                string formUrl = "http://quanapi.sinaapp.com/fetion.php";
                string formData = url;                               //提交的参数

                //注意提交的编码 这边是需要改变的 这边默认的是Default：系统当前编码
                byte[] postData = Encoding.UTF8.GetBytes(formData);



                // 设置提交的相关参数 
                HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
                Encoding myEncoding = Encoding.UTF8;
                request.Method = "POST";
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                request.ContentLength = postData.Length;

                // 提交请求数据 
                System.IO.Stream outputStream = request.GetRequestStream();
                outputStream.Write(postData, 0, postData.Length);
                outputStream.Close();

                HttpWebResponse response;
                Stream responseStream;
                StreamReader reader;
                string srcString;
                response = request.GetResponse() as HttpWebResponse;
                responseStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                srcString = reader.ReadToEnd();
                string result = srcString;   //返回值赋值
                reader.Close();
                return result;
            }
            catch
            {
                return "error";
            }
        }






        /// <summary>
        /// Aliyun短信发送
        /// </summary>
        /// <param name="accessKeyId"></param>
        /// <param name="accessKeySecret"></param>
        /// <param name="RecNum">接收号码，多个号码可以逗号分隔</param>
        /// <param name="TemplateCode">管理控制台中配置的审核通过的短信模板的模板CODE（状态必须是验证通过）</param>
        /// <param name="ParamString">短信模板中的变量；数字需要转换为字符串；个人用户每个变量长度必须小于15个字符。</param>
        /// <param name="SignName">管理控制台中配置的短信签名（状态必须是验证通过）</param>
        /// <param name="res"></param>
        /// <returns></returns>
        public static bool AliyunSmsSend(string regionId, string accessKeyId, string accessKeySecret, string RecNum, string TemplateCode, string ParamString, string SignName, out string res)
        {
            res = "";

            IClientProfile profile = DefaultProfile.GetProfile(regionId, accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            try
            {
                request.SignName = SignName;
                request.TemplateCode = TemplateCode;
                request.RecNum = RecNum;
                request.ParamString = ParamString;
                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);
                return true;
            }
            catch (ServerException e)
            {
                res = e.Message;
            }
            catch (ClientException e)
            {
                res = e.Message;
            }
            return false;
        }

    }
}
